<template>
  <div>
    <div>
      <el-form :inline="true" :model="form">
        <el-button-group>
          <el-button type="primary" icon="plus" @click="addHandler"/>
          <el-button type="primary" icon="edit" @click="editHandler"/>
        </el-button-group>
      </el-form>
    </div>

    <el-table :data="getOrganBao" height="400" border :stripe="true" :highlight-current-row="true"
              @current-change="tableRowChangeClickHandler"
              style="width: 100%">
      <el-table-column prop="fundCode" label="基金代码" min-width="15%"/>
      <el-table-column prop="fundName" label="基金名称" min-width="15%"/>
      <el-table-column prop="minValue" label="最低起购" min-width="15%"/>
      <el-table-column prop="reminder" label="提示语" min-width="25%"/>
      <el-table-column prop="isRecommend" label="是否推荐" min-width="15%" :formatter="formatterRecommendHandler"/>
      <el-table-column prop="isValid" label="状态" min-width="15%" :formatter="formatterStateHandler"/>
      <el-table-column inline-template :context="_self" width="100" label="操作" >
      <span>
        <el-button :type="row.isValid === false?'danger':'info'" size="small"
                   @click="onlineHandler($index,row)">{{row.isValid === false?'上线':'下线'}}</el-button>
      </span>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getCurrentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="getCount">
      </el-pagination>
    </div>

    <upload-form ref="uploadForm" :mode="formMode" :param="currentRecord"/>

    <!--<button v-clipboard="copyData" @success="copySucHandler" @error="copyErrorHandler"-->
    <!--ref="btnCopy" style="display:none">copy-->
    <!--</button>-->


  </div>
</template>

<script type="text/babel">
  import {mapActions} from 'vuex'

  import organBaoStore from './bll/organBaoStore'
  import store from '../../store'

  (!store.state.organBaoStore) && store.registerModule('organBaoStore', organBaoStore)


  import UploadForm from './uploadForm.vue';


  export default {
    data() {
      return {
        form: {
          region: ''
        },
        formMode: null,
        currentRecord: null,
        pageSize: 10,
        pageIndex: 1
        //form: {},
      }
    },
    components: {UploadForm},
    computed: {
      getOrganBao() {
        return this.$store.state.organBaoStore.organBao;
      },

      getCount() {
        return this.$store.state.organBaoStore.totalCount;
      },
      getCurrentPage() {
        return this.$store.state.organBaoStore.currentPage;
      }
    },

    mounted() {
      console.log('file upload page mounted.');
      this.initPage();
    },

    methods: {
      ...mapActions(['queryOrganBao', 'online', 'queryOnlineOrganBao']),

      initPage() {
        this.queryOrganBao({pageIndex: this.pageIndex, pageSize: this.pageSize});
      },
      openUploadDialogHandler() {
        this.$refs.uploadDialog.open();
      },
      addHandler() {
        this.$refs.uploadForm.enable();
        this.$refs.uploadForm.enableCode();
        this.formMode = 'ADD';
        this.$refs.uploadForm.clear();
      },
      editHandler() {
        if (!this.currentRecord) {
          this.$message.warning('请选择一条记录');
          return;
        }

        this.$refs.uploadForm.enable();
        this.formMode = 'EDIT';
      },


      tableRowChangeClickHandler(currentRow, oldCurrentRow) {
        console.log(currentRow);
        this.currentRecord = currentRow;
        this.$refs.uploadForm.disable();
      },

      formatterRecommendHandler (row, column) {
        switch (column.property) {
          case 'isRecommend':
            if (row.isRecommend) {
              return '推荐'
            } else {
              return '不推荐'
            }
        }
        return 'Unkown key'
      },
      formatterStateHandler (row, column) {
        switch (column.property) {
          case 'isValid':
            if (row.isValid) {
              return '上线'
            } else {
              return '未上线'
            }
        }
        return 'Unkown key'
      },
      onlineHandler(index, row) {
        this.$confirm(row.isValid === false ? '你确定要上线吗?' : '你确定要下线吗?', '确定', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let s = 0;
          if (row.isValid === false) {
            s = 1
          }
          this.online({fundCode: row.fundCode, state: s}).then(data => {
            this.$message.success(row.isValid === false ? '下线成功' : '上线成功');
            this.queryOnlineOrganBao();
          });
        });
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val;
        this.queryOrganBao({pageIndex: 1, pageSize: val});
      },
      handleCurrentChange(val) {
        console.log(arguments);
        console.log(`current${val}`);
        this.pageIndex = val;
        this.queryOrganBao({pageIndex: val, pageSize: this.pageSize});

      },
      addcombinationHandler() {
        if (!this.currentRecord) {
          this.$message.warning('请选择需要修改的内容');
          return;
        }
        console.log(this.currentRecord)
        this.$router.push({
          path: '/wareHouse/add',
          query: {warehouseId: this.currentRecord.warehouseId}
        })
      }
    }
  }
</script>

<style lang="scss">
  .download-url {
    padding: 5px;
  }

  .qrcode-wrapper {
    margin: auto;
  }
</style>

